Unity work

Group project

Sun Of A Gun

This is a project I had about 6 weeks to work on with a group of 4 others, with me being assigned to code the entire game from the Health mechanics to combat. the software used was Unity mainly and Visual Studio for coding.

Overview

the parameters for this project was for it to be mostly completed within 6 weeks and the final product to be of medium fedelity prototype. The main theme of the game would have to be based around renewable energy with the group being able to decide the genre of the game and style. We were assigned into groups of 5-6 with set roles decided in a group descussion with me being assigned as the only coder to code the whole game from the ground up in a white box. at the end I ended up completing the base games mechanics but was left without assets so the final product was left with the white box. however most of the coding done was from myself with only some help from acceessable youtube tutorials to get an understanding of what I needed to do. Click the photo to download and have a go at the game!

Player movement

at the start gets the players rigidbody for jumping and sets the current speed to the defult speed veriable also in updates it is always checking the coroutine to see if it should run based off of if statements and also looks out for a bool that will stop the moveplayer coroutine if true for a set amount of time.

Checking for a collision with the players capsule collider and if whatever the player collided withs tag is "Hazard" the if statement will run setting Ouch to =true which will stop the player and enemy movement temporarily accourding to the coroutine(stop) which will run for 0.5 seconds then set ouch to false allowing everything to move again. also the if statement moves the player left by a certain amount to stop staying in the collider.

the (Flip) routine flips the player model around depending on which direction it is moving in by transforming the models scale by times -1 which either makes it negative making it face a certain way or a negative times a negative makes a posative making it face the opposite direction. with the ontriggerstay it is used to change the players speed as long as it is staying in smog and isnt under a light.

The code for the basic movement of the player with it getting the Horizontal input for left and right movement instead of assigning each key to a certain direction and using the asigned speed times by the Time.deltatime to figure out how fast the character should be moving with Time.deltatime being used so the character wouldnt move faster or slower depending on framerate. with jumping it was a bit more complicated with me having to apply a force to the player to make the jump somewhat more relaistic with an arc, and with only an impule force as to only apply the force once momentarily to stop the player from flying away also there is a ground check on the if statment as to not let the player jump continuously. the if statements for the dashing require 2 buttons to be pressed or held down together and that canDash = true as to stop back to back dashing. the if statements for flipping work by detecting if the HorizontalInput is posative or negative as going left is -1 and right is 1 which then lets the program know which way they should be flipped.

The IsGrounded check uses an empty object at the feet of the playermodel to check if it is actively colliding with a layer called ground if it is Isgrounded = true and the player can jump and proform other mechanics that require the player to be Grounded. with the dashing it uses a veriaty of veriables to achive with it setting candash to false to stop it from being activated again till the code ends and canshoot = false to stop the player from shooting as they move incase for self damage that was an issue up untill the end of the project where i managed to find a way to stop it. also turns gravity off for the player during the dash as to allow more freedom of movement with it. the Dash coroutine also uses a lot of waitforseconds as to have accurate timings when i want the dash to stop and when gravity should be reaplied as to have the player stop more naturally. the same if applied for dashing left with the only differance being the transform.localscale being negative as to move left of the screen.
The OnTriggerEnter is used for detecting if the players collider enters a trigger and if those triggers corresponds to either "Key" where it changed the HasKey to true which allows the character to open hatches which require to collide with the player and the bool to =true and or the tag "light" where it sets InSmog to false which is caused by the Smog model which when true slows down the player by changing the currentSpeed where as when in a "light" tag where the speed gets reset

Health and Damage

At the start it sets the current health to a predetermined maxhealth which can be changed within unity itself as this scriped can be applied to anything that needs health. the if statements tells whatever the script is attached to deastroy itself when their health is less than or equal to zero and theres a set on for player incase I wanted to run a game overscreen when the player specifically died. the takedamage routine takes away the assigned damage value away from the current health, the actual Damagevalue is determined from the damage script. HealDamage value also comes from another specified script however in this one it has a mathf.Clamp as to not let the player get to get over the maxhealth.
The damage script lets anything it is attached to take damage as long as it completes the if statements. Bullet is assigned to the projectile the player fires which allows them to do damage to anything with the script and health script attached to it as long as it isnt tagged as the "player" where as the other if statement only works for enemies attacking the player as they can only damage anything not tagged "Hazard" as they used to kill each other every so often which wasnt meant to occur. all of these referance the health script and to tell the takeDamage routine in the script how much damage it should take base on what you set as the damage for this script in unity.

This script is to controll the ammo for the players gun as the player actually has 2 different gun for shooting straight ahead and also shooting down so to properly have a single ammo counter instead of one for each gun I needed a seperate script for them. this script just sets current ammo to the max ammo at the start and takes away 1 every time the gun is shot and for reloading the currentammo gets reset and it starts a coroutine where you cannot shoot again until it is over

This script applies to the bullets itself and tells the bullet when it needs to be destroyed either when the life runs out which is 1.5f or when it hits something tagged "Hazard"

The lightControl script allows the player to heal themselves under it with the assigned healthvalue within the script once whenever the player is in the trigger "light" it also sets InSmog false as to restore the players movement speed. the else if is for if the player is already at max health it doesnt run the code.

Enemy Controller

the first piece of code is if ouch from playermovement = true then the enemy this is attached to will also stop moving. when the enemy reaches the max position it switches the Turn bool to true and also flips the model with the same code as with the player. when turn is true it tells the code to run the coroutine moveRight and vice versa when turn is false.

The moveRight and moveLeft routines simply make the enemy constantly move in the set direction at the speed set in the veriables beforehand until they reach the max or min position where it activates the other routine and flips the character model.

The script for the second enemyController starts with getting the players position through looking at the transform of what ever is tagged "player" and also getting its own rigidbody for later code enabling jumping then theres also the if ouch true code snippet to stop the enemy if the player has been hit as not to instantly kill the player in some scenarios. the code then contstantly gets the distance from the player and also from a box that the player can move around by comparing its position and the player / box's position. then the next if statement requires the player's x position to be more thank its own then it will execute the code which makes it first turn to face right by once again flipping the scalling of the model by negative 1 and makes the enemy move in that direction towards the player. same applys for the else if but for moving left. there is also an activation distance that is set at the start amd if the distance from the player is more than it the code stops the enemy from moving and only when the player walks into the activation radius the enemy can start to move to attack them.

theres also similar code for the distance from the bos however in this case if the box is close enought to the enemy it can jump over it as before hand they were easy to deal with as they could get stuck behind the box the if statements require a ground check before it can execute the jump coroutine as otherwise the enemy would keep jumping constantly until the box was out of the activation radius. then theres another methode to stop the enemy from moving once the player is hit by detecting if iit has collided with something tagged with "Player" and when it does the coroutine (Wait) starts where it waits for 1 second using an ienumerator.

the Groundcheck is coded the same as with the player having a empty object bellow the enemy model checking for a collision with a layer called "ground" and the jump uses the rigidbody of the enemy model we got earlier and applies force momentarily upwards like how it was also done in the player movement scripts.

The code for the EnemyControl3 script was mostly all done by my lecturer Robert Blofield in his Computer Game Design Principles class during week 10 of semester 1 where we got taught state machines and step by step copied most of the code while fixing some intentional errors that were designed to challenge us stats work by setting what will happen when running the state like for example in the code for in enemystate.Firing when in the state it runs code to fire projectiles at the player where as in the void update you can set if statement for conditions when to switch states this is more advanced and efficiant than using multiple bools to do the same thing. the only code within the script that was not done by Rob would be the player flipping which was the same code as used in my other scripts to flip player models as the 3D rotation using quaternion slerp that was provided did not work with my game.

Miscellaneous Scripts

The barrel controls makes it so when it is applied to something with the tag "barrel" and gets hit by something with the tag "Bullet" it destroys the barrel and in its position it instantiates a key prefab with all the code it needs.

this code is for the camera to follow the player in a smooth way that doesnt hurt the eyes when there is sudden and fast movement the mathf.Clamp keeps the camera from going out of bounds and the targetPosition adds and offset as to trail the player slightly behind and then to actually follow the player it has a transfrom.position with vector3 smoothdamp so the camera is smooth and smoothtime so the camera doesnt snap to the player

This image is the combanation of 2 scripts both the hatch control and glass break control. for the hatch control when it collides with something tagged "rock" it will be destroyed. and the hatch also gets destroyed by it has to meet the requirements of both it being the player and HasKey must equal true, when the code is run HasKey gets set false so another key can be picked up and also so the player can run through the levels without trying to find the key.

This code if for the players shooting with the first if statement meant for stopping the player from being able to shoot is canShoot = false then it runs the basic (Shoot) routine then we have two if statements to switch the gun the player is using depending if the player is cureently on the ground and is pressing the S key to shoot down it works by setting 2 booleans one true one false depending on if the player if grounded or not, when the maingun = true it sets the maingun model to true which basically makes the other corresponding gun model none functional and invisable so none of the code apllied works which is why there needed to be 2 guns with the code on both. when secondarygun = true the main gun is set as false which makes the gun1 model disappear and the Gun model 2 appear and functional. The next If statement makes it so when a projectile prefabs X position is more than or less than the designated values the prefabs get destroyed. also there is an ontriggerenter for when the gun enters a trigger tagged with "light" to set lightsource as true which allows the gun to reload
the (Shoot) routine is 3 If statments for which direction to shoot in and shooting down with a long laundry list of requirement before the code within will run with all needing space to be pressed and at least 1 currentammo for normal shooting they also take in which direction the player is facing to shoot in the right direction where as with shooting down it does a ground check and if its false and you meet the other requirements and also pressing S at the same time then you can shoot down and change which gun model is being used. to shoot they all use the same code with instantiating the Bullet prefab from a set firepoint which is decided before hand and when run current ammo -1 and runs a coroutine of (wait) so you cant spam fire. the only difference between the first two if statements is when it makes the Bullet prefab to move for firing to the left the firepoint is negative so the bullet doesnt just spawn there and keep going right.